/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.time;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.time.EleSourceTime;
import com.thinkgem.jeesite.modules.source.service.time.EleSourceTimeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 时间资源管理Controller
 * @author zc
 * @version 2017-04-20
 */
@Controller
@RequestMapping(value = "${adminPath}/source/time/eleSourceTime")
public class EleSourceTimeController extends BaseController {

	@Autowired
	private EleSourceTimeService eleSourceTimeService;
	
	@ModelAttribute
	public EleSourceTime get(@RequestParam(required=false) String id) {
		EleSourceTime entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceTimeService.get(id);
		}
		if (entity == null){
			entity = new EleSourceTime();
		}
		return entity;
	}
	
	@RequiresPermissions("source:time:eleSourceTime:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceTime eleSourceTime, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleSourceTime> page = eleSourceTimeService.findPage(new Page<EleSourceTime>(request, response), eleSourceTime); 
		model.addAttribute("page", page);
		return "modules/source/time/eleSourceTimeList";
	}

	@RequiresPermissions("source:time:eleSourceTime:view")
	@RequestMapping(value = "form")
	public String form(EleSourceTime eleSourceTime, Model model) {
		model.addAttribute("eleSourceTime", eleSourceTime);
		return "modules/source/time/eleSourceTimeForm";
	}

	@RequiresPermissions("source:time:eleSourceTime:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceTime eleSourceTime, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleSourceTime)){
			return form(eleSourceTime, model);
		}
		eleSourceTimeService.save(eleSourceTime);
		addMessage(redirectAttributes, "保存时间资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceTime/?repage";
	}
	
	@RequiresPermissions("source:time:eleSourceTime:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceTime eleSourceTime, RedirectAttributes redirectAttributes) {
		eleSourceTimeService.delete(eleSourceTime);
		addMessage(redirectAttributes, "删除时间资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceTime/?repage";
	}

	@RequiresPermissions("source:time:eleSourceTime:check")
	@RequestMapping(value = "check")
	public String check(EleSourceTime eleSourceTime, RedirectAttributes redirectAttributes) {
		eleSourceTimeService.check(eleSourceTime);
		addMessage(redirectAttributes, "审核时间资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceTime/?repage";
	}

}